// ClipYahooRoster.js - (c) Glenn Carr
//
// Grabs a cleanly formatted Yahoo fantasy roster on the current page
// and puts it on the clipboard.  Tested with IE 6,7, Firefox 2.
//
ClipRoster = {
    init: function()
    {
        var clipStrings = new Array();
        var clipStringsCondensed = new Array();
        for ( var iTable = 0; ; iTable++ )
        {
            var statTable = document.getElementById('statTable' + iTable);
            if ( statTable == null )
                break;
                
            var positions = new Object();
            
            for ( var iRow = 0; iRow < statTable.rows.length; iRow++ )
            {
                var row = statTable.rows[ iRow ];
                var rowClass = getClassName( row );
                if ( rowClass == 'headerRow1' )
                {
                    for ( var iCell = 0; iCell < row.cells.length; iCell++ )
                    {
                        var cell = row.cells[ iCell ];
                        var cellClass = getClassName( cell );
                        if ( cellClass == 'player' )
                        {
                            var category = stripTags( cell.innerHTML );
                            clipStrings.push( category );
                            clipStrings.push( '\r\n' );

                            clipStringsCondensed.push( category );
                            clipStringsCondensed.push( '\r\n' );
                            break;
                        }
                    }                   
                }
                else if ( /^(odd|even)/i.test( rowClass ) )
                {
                    for ( var iCell = 0; iCell < row.cells.length; iCell++ )
                    {
                        var cell = row.cells[ iCell ];
                        var cellClass = getClassName( cell );
                        if ( /^pos/i.test( cellClass ) )
                        {
                            var playerPos = stripTags( cell.innerHTML );
                            if ( !positions.hasOwnProperty( playerPos ) )
                                positions[ playerPos ] = new Array();
                            clipStrings.push( playerPos );
                            clipStrings.push( ' - ' );
                        }
                        else if ( getClassName( cell ) == 'player' )
                        {
                            var playerName = stripAccent( stripTags( cell.innerHTML ) ).replace( /\r\n|^\s+|\s+$/gi, '' ).replace( /\s+/g, ' ' );
                            positions[ playerPos ].push( playerName );
                            clipStrings.push( playerName  );
                            clipStrings.push( '\r\n' );
                            break;
                        }
                    }                   
                } 
            }
            clipStrings.push( '\r\n' );

            for ( var pos in positions )
            {
                clipStringsCondensed.push( pos );
                clipStringsCondensed.push( ': ' );
                var players = positions[ pos ];
                var delim = '';
                for ( var p = 0; p < players.length; p++ )
                {
                    clipStringsCondensed.push( delim );
                    clipStringsCondensed.push( players[ p ].replace( /\(\w+\s+- /, '(' ).replace( /\)\s+DL/i, ')' ) ); // remove team abbreviation and DL indicator
                    delim = ', ';
                }
                clipStringsCondensed.push( '\r\n' );
            }
            clipStringsCondensed.push( '\r\n' );
        }

        var rosterText = clipStrings.join( '' );
        var condensedRosterText = clipStringsCondensed.join( '' );

        if ( rosterText.replace( /\r\n|\s+/gi, '' ) != '' )
        {
            if ( copy( rosterText ) )
                alert( 'The following roster has been copied to the clipboard:\r\n\r\n' + rosterText );
        }
        else
        {
            alert( 'This page doesn\'t appear to contain a Yahoo fantasy roster.' );
        }
    }
};

function copy(text2copy) 
{
    if (window.clipboardData) 
    {
        return window.clipboardData.setData("Text",text2copy);
    } 
    else 
    {
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) 
        {
            var divholder = document.createElement('div');
            divholder.id = flashcopier;
            document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="http://glenncarr.com/yahoofantasy/_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
    }
    return true;
}

function stripTags(s)
{
    return s.replace(/<\/?[^>]+>/gi,'');
}

/* Based on code by (C)Stephen Chalmers
* Strips grave, acute & circumflex accents
* http://www.thescripts.com/forum/thread145532.html
*/
function stripAccent(str)
{
    var s=str;

    var rExps=[ 
    /[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
    /[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
    /[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
    /[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
    /[\xD9-\xDB]/g, /[\xF9-\xFB]/g, 
    /[\xD1]/g, /[\xF1]/g ];

    var repChar=['A','a','E','e','I','i','O','o','U','u','N','n'];

    for(var i=0; i<rExps.length; i++)
        s=s.replace(rExps[i],repChar[i]);

    return s;
}

function getClassName( el )
{
    var className = el.getAttribute( 'class' );
    if ( className == null )
        className = el.getAttribute( 'className' );
    return className;
}

ClipRoster.init();
